// miniprogram/pages/run/run.js
var countTooGetLocation = 0;
var total_micro_second = 0;
var starRun = 0;
var totalSecond  = 0;
var oriMeters = 0.0;
/* 毫秒级倒计时 */
function count_down(that) {

  if (starRun == 0) {
    return;
  }

  if (countTooGetLocation >= 100) {
    var time = date_format(total_micro_second);
    that.updateTime(time);
  }

  if (countTooGetLocation >= 5000) { //1000为1s
      that.getLocation();
      countTooGetLocation = 0;
  }   
  setTimeout(function(){
  countTooGetLocation += 10;
  total_micro_second += 10;
  count_down(that);
  }
  ,10
  )
}
// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;


return hr + ":" + min + ":" + sec + " ";
}


function getDistance(lat1, lng1, lat2, lng2) { 
  var dis = 0;
  var radLat1 = toRadians(lat1);
  var radLat2 = toRadians(lat2);
  var deltaLat = radLat1 - radLat2;
  var deltaLng = toRadians(lng1) - toRadians(lng2);
  var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
  return dis * 6378137;

  function toRadians(d) {  return d * Math.PI / 180;}
} 

function fill_zero_prefix(num) {
return num < 10 ? "0" + num : num
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clock: '',
    isLocation:false,
    latitude: 0,
    longitude: 0,
    markers: [],
    covers: [],
    meters: 0.00,
    time: "0:00:00",
    totalTime:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this; 
//  获取当前定位的经纬度信息
wx.showLoading({
  title:"定位中",
  mask:true
})
wx.getLocation({
  type: 'gcj02',
  altitude:true,//高精度定位
  //定位成功，更新定位结果
  success: function (res) {
    var latitudee = res.latitude
    var longitudee = res.longitude
    that.setData({
      longitude:longitudee,
      latitude: latitudee,
    })
    console.log(that.data.markers);
  },
  //定位失败回调
  fail:function(){
    wx.showToast({
      title:"定位失败",
      icon:"none"
    })
  },
  complete:function(){
    //隐藏定位中信息进度
    wx.hideLoading()
  }
})
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(this.data.meters);
    this.getLocation()
    console.log("onLoad")
    count_down(this);
  },
  starRun :function () {
    if (starRun == 1) {
      return;
    }
    starRun = 1;
    count_down(this);
    this.getLocation();
  },

  stopSports:function(){
    starRun = 0;
    count_down(this);
    let slef =this
    console.log('stop----------');
    console.log(slef.data.meters);
    slef.setData({
      totalTime:total_micro_second
    })
    if(slef.data.meters>=0.00){
      wx.cloud.callFunction({
        name:'UpLoadmeters',
        data:{
          meters:slef.data.meters,
          time:slef.data.time,
          totalTime:slef.data.totalTime
        },
        success(res){
          console.log(res);
        }
      })
    }
    console.log(total_micro_second);
    setTimeout(function(){
      wx.switchTab({
        url: `/pages/my/my`,
      })
    } 
    ,1000
    )
  },

 //****************************
  stopRun:function () {
    starRun = 0;
    count_down(this);
  },


//****************************
  updateTime:function (time) {

    var data = this.data;
    data.time = time;
    this.data = data;
    this.setData ({
      time : time,
    })

  },


//****************************
  getLocation:function () {
    var that = this
    wx.getLocation({

      type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function(res){
        console.log("res----------")
        console.log(res)

        //make datas 
        var newCover = {
            latitude: res.latitude,
            longitude: res.longitude,
            iconPath: '../../statics/image/redPoint.png',
          };
        var oriCovers = that.data.covers;
        
        console.log("oriMeters----------")
        console.log(oriMeters);
        var len = oriCovers.length;
        var lastCover;
        if (len == 0) {
          oriCovers.push(newCover);
        }
        len = oriCovers.length;
        var lastCover = oriCovers[len-1];
      
        console.log("oriCovers----------")
        console.log(oriCovers,len);

        var newMeters = getDistance(lastCover.latitude,lastCover.longitude,res.latitude,res.longitude)/1000;
        
        if (newMeters < 0.0015){
            newMeters = 0.0;
        }

        oriMeters = oriMeters + newMeters; 
        console.log("newMeters----------")
        console.log(newMeters);


        var meters = new Number(oriMeters);
        var showMeters = meters.toFixed(2);
        oriCovers.push(newCover);
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [],
          covers: oriCovers,
          meters:showMeters,
        });
        console.log(that.data.covers);
      },
    })
   
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})